package com.im.partchat.topic.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.im.partchat.App;
import com.im.partchat.R;
import com.im.partchat.topic.bean.AvatarBean;
import com.im.partchat.topic.bean.TopicBean;
import com.im.partchat.ui.activity.GalleryActivity;
import com.im.partchat.utils.ValueUtil;

import java.util.ArrayList;
import java.util.List;

public class PictureGridLayout extends GridLayout {

	private int pictureSize;
	
	public PictureGridLayout(Context context){
		super(context);
		initView(context);
	}
	
	public PictureGridLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		// TODO Auto-generated method stub
		Activity activity = (Activity) context;
		WindowManager wm = activity.getWindowManager();
		int margin = (int)context.getResources().getDimension(R.dimen.topic_gridphoto_leftmargin);
		pictureSize = (wm.getDefaultDisplay().getWidth() - 2 *margin )/3;
		setRowCount(3);
	}
	
	public void setPictures(final Context context, final List<AvatarBean> pictures, TopicBean bean){
		removeAllViews();
		if (pictures == null) {
			setVisibility(View.GONE);
			return ;
		}
		
		if(pictures.size() == 4){
			setColumnCount(2);
		}else{
			setColumnCount(3);
		}
		
		switch (pictures.size()) {
		case 0:
			//无图片
			setVisibility(View.GONE);
			break;
		case 1:
			setVisibility(View.VISIBLE);
			//一张图，按照服务器返回的宽高等比例缩放大小
			View layout =  LayoutInflater.from(context).inflate(R.layout.griditem_imageview,null);
			ImageView imageview =  (ImageView)layout.findViewById(R.id.imageview);
			addView(layout);
			ValueUtil.resetLayoutParams(context,layout, bean);

            Glide.with(App.getApplication())
                    .load(pictures.get(0).findOriginalUrl())
                    .error(R.drawable.rc_image_default)
                    .into(imageview);
			
			imageview.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					intentToGallery(context, pictures,0);
				}
			});
			break;
		default:
			setVisibility(View.VISIBLE);
			for (int i = 0; i < pictures.size(); i++) {
				AvatarBean avatarBean = pictures.get(i);
				View layout2 =  LayoutInflater.from(context).inflate(R.layout.griditem_imageview,null);
				ImageView imageviews =  (ImageView)layout2.findViewById(R.id.imageview);
				addView(layout2);

				layout2.getLayoutParams().width = pictureSize;
				layout2.getLayoutParams().height = pictureSize;

				Glide.with(App.getApplication())
						.load(pictures.get(0).findOriginalUrl())
						.error(R.drawable.rc_image_default)
						.into(imageviews);
				final int index = i;
				imageviews.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						intentToGallery(context, pictures,index);
					}
				});
			}
			break;
		}
	}
	
	private void intentToGallery(final Context context,final List<AvatarBean> pictures,int index) {
		Intent intent = new Intent(context, GalleryActivity.class);
		ArrayList<String> urls = new ArrayList<>();
		for (AvatarBean bean : pictures) {
			urls .add(bean.findOriginalUrl());
		}
		intent.putExtra(GalleryActivity.EXTRA_IMAGE_URLS, urls);
		intent.putExtra(GalleryActivity.EXTRA_IMAGE_INDEX, index);
		context.startActivity(intent);
	}
}
